package com.ticker.national.db;

import java.util.HashMap;
import java.util.Map;

public enum SchemeCollection {
	PROFILE(ProfileDB.TABLE_NAME, ProfileDB.columnNames, ProfileDB.columnTypes),
	PROFILE_IMAGE(ProfileImageDB.TABLE_NAME, ProfileImageDB.columnNames, ProfileImageDB.columnTypes),
	LOCATION_IMAGE(LocationImageDB.TABLE_NAME, LocationImageDB.columnNames, LocationImageDB.columnTypes),
	POSITION(PositionDB.TABLE_NAME, PositionDB.columnNames, PositionDB.columnTypes),
	POSITION_JOIN(PositionJoinDB.TABLE_NAME, PositionJoinDB.columnNames, PositionJoinDB.columnTypes);
	
    private String tableName;
    private String[] columnNames;
    private String[] columnTypes;
    
	private SchemeCollection(String tableName, String[] columnNames, String[] columnTypes) {
        this.tableName = tableName;
        this.columnNames = columnNames;
        this.columnTypes = columnTypes;
	}
	
    // Convenient list to help getting
    private static final Map<String, SchemeCollection> tables = new HashMap<String, SchemeCollection>();
    private static final Map<String, String> queriesCreateTable = new HashMap<String, String>();
    private static final Map<String, String> queriesDropTable = new HashMap<String, String>();
    private static final Map<String, String> queriesSelectTable = new HashMap<String, String>();
    
	// table name
	static {
		for(SchemeCollection table: SchemeCollection.values()) {
			tables.put(table.tableName, table);
		}
	}
	
    // table creation query
    static {
        for (SchemeCollection table : SchemeCollection.values()) {
            StringBuffer query = new StringBuffer();
            query.append("CREATE TABLE ");
            query.append(table.tableName);
            query.append(" ( ");
            for (int colNum = 0; colNum < table.columnNames.length; colNum++) {
                query.append(table.columnNames[colNum]);
                query.append(" ");
                query.append(table.columnTypes[colNum]);
                query.append(" ");
                if (colNum < (table.columnNames.length - 1)) {
                    query.append(", ");
                }
            }
            query.append(") ");
            queriesCreateTable.put(table.tableName, query.toString());
        }
    }
    
    // table drop query
    static {
        for (SchemeCollection table : SchemeCollection.values()) {
            StringBuffer query = new StringBuffer();
            query.append("DROP TABLE IF EXISTS ");
            query.append(table.tableName);
            queriesDropTable.put(table.tableName, query.toString());
        }
    }

    // table select query
    static {
        for (SchemeCollection table : SchemeCollection.values()) {
            StringBuffer query = new StringBuffer();
            query.append("SELECT ");
            for (int colPos = 0; colPos < table.columnNames.length; colPos++) {
                query.append(table.columnNames[colPos]);
                if (colPos < (table.columnNames.length - 1)) {
                    query.append(", ");
                }
            }
            queriesSelectTable.put(table.tableName, query.toString());
        }
    }
    
    public static Map<String, String> getQueriesCreateTable() {
        return queriesCreateTable;
    }

    public static Map<String, String> getQueriesDropTable() {
        return queriesDropTable;
    }

    public static Map<String, String> getQueriesSelectTable() {
        return queriesSelectTable;
    }
    
    public String getTableName() {
    	return this.tableName;
    }
}
